package cn.hairui.blog.controller.manage;

import cn.hairui.blog.constant.PubConstant;
import cn.hairui.blog.model.Category;
import cn.hairui.blog.model.User;
import cn.hairui.blog.service.CategoryService;
import com.alibaba.druid.support.json.JSONUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

/**
 * <p>
 * <h2>请添加说明
 * </h2>
 * <ol>请添加详细说明
 * </ol>
 *
 * <h2>修改历史
 * </h2>
 * <ol>如有修改，请添加修改历史
 * </ol>
 *
 * </p>
 *
 * @author laoganbu
 * @version V1.0
 * @date 2019/8/9 16:36
 */
@Controller
@RequestMapping(value = "/manage")
public class CategoryManageController {

    @Autowired
    private CategoryService categoryService;

    //列表页面
    @RequestMapping(value = "page/category/categoryList")
    public String categoryList() {
        return "manage/page/category/categoryList";
    }

    //新增页面
    @RequestMapping(value = "page/category/categoryAdd")
    public String categoryAdd() {
        return "manage/page/category/categoryAdd";
    }

    //修改页面
    @RequestMapping(value = "page/category/categoryUpdate")
    public String categoryUpdate(HttpServletRequest request, Model model) {
        int id = Integer.parseInt(request.getParameter("id"));
        Category category = categoryService.find(id);
        model.addAttribute("category", category);
        return "manage/page/category/categoryUpdate";
    }

    //列表数据
    @RequestMapping(value = "page/category/list")
    @ResponseBody
    public String list(HttpServletRequest request) {
        Map map = new HashMap<Object, Object>();
        int pageSize = 5;//默认每页显示5条记录
        int pageNum = 1;
        try {
            pageNum = Integer.parseInt(request.getParameter("page"));
            pageSize = Integer.parseInt(request.getParameter("limit"));
        } catch (Exception e) {
        }
        PageHelper.startPage(pageNum, pageSize);
        List<Map> categoryList = categoryService.listMgr();

        PageInfo<Map> pageInfo = new PageInfo<Map>(categoryList);
        map.put("data", categoryList);
        map.put("code", 0);
        map.put("count", pageInfo.getTotal());
        return JSONUtils.toJSONString(map);
    }


    //新增数据
    @RequestMapping(value = "page/category/add")
    @ResponseBody
    public String add(HttpServletRequest request, Category category) {
        Map map = new HashMap<Object, Object>();
        //处理分类置顶
        if (PubConstant.SWITCH_ON.equals(category.getIsShow())) {
            category.setIsShow(PubConstant.YES_NO_Y);
            int maxOrder = categoryService.findMaxOrder();
            category.setShowOrder(maxOrder + 1);
        } else {
            category.setIsShow(PubConstant.YES_NO_N);
        }
        int serno = categoryService.add(category);
        Logger.getLogger(this.getClass().getName()).info("新增分类编号：" + serno);
        map.put(PubConstant.flag, PubConstant.success);
        return JSONUtils.toJSONString(map);
    }

    //更新数据
    @RequestMapping(value = "page/category/update")
    @ResponseBody
    public String update(HttpServletRequest request, Category category) {
        Map map = new HashMap<Object, Object>();
        //处理专题编号
        if (PubConstant.SWITCH_ON.equals(category.getIsShow())) {
            category.setIsShow(PubConstant.YES_NO_Y);
        } else {
            category.setIsShow(PubConstant.YES_NO_N);
            category.setShowOrder(null);
        }
        categoryService.update(category);
        map.put(PubConstant.flag, PubConstant.success);
        return JSONUtils.toJSONString(map);
    }

    //删除数据
    @RequestMapping(value = "page/category/delete", method = RequestMethod.POST)
    @ResponseBody
    public String delete(@Param(value = "id") int id, HttpServletRequest request) {
        Map map = new HashMap();
        //判断权限
        User userinfo = (User) request.getSession().getAttribute(PubConstant.GLOBAL_SESSION_NAME);
        if (userinfo == null || !PubConstant.YES_NO_Y.equals(userinfo.getAdminflag())) {
            //普通用户或者游客 无权限删除文章
            map.put(PubConstant.flag, PubConstant.permissions);
        } else {
            //管理员
            Category category = new Category();
            category.setId(id);
            categoryService.delete(category);
            map.put(PubConstant.flag, PubConstant.success);
        }
        return JSONUtils.toJSONString(map);
    }

    //首页展示
    @RequestMapping(value = "page/category/categoryshow")
    @ResponseBody
    public String categoryshow(HttpServletRequest request) {
        Map map = new HashMap<Object, Object>();
        int id = Integer.parseInt(request.getParameter("id"));

        Category category = categoryService.find(id);
        String flag = "";
        if (PubConstant.YES_NO_Y.equals(category.getIsShow())) {
            //取消置顶
            flag = PubConstant.YES_NO_N;

        } else {
            //置顶
            flag = PubConstant.YES_NO_Y;
        }
        categoryService.updateIsShow(id, flag);
        map.put(PubConstant.flag, flag);
        return JSONUtils.toJSONString(map);
    }

    //首页展示置顶
    @RequestMapping(value = "page/category/categorytop")
    @ResponseBody
    public String categorytop(HttpServletRequest request) {
        Map map = new HashMap<Object, Object>();
        int id = Integer.parseInt(request.getParameter("id"));
        //TODO 置顶算法
        categoryService.updateShowOrder(id, PubConstant.ONE);
        map.put(PubConstant.flag, PubConstant.success);
        return JSONUtils.toJSONString(map);
    }
}
